﻿#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
#endregion

namespace ProjectRoughWork.Engine.SoundManager
{
    class SoundManager : ISoundManager
    {
        // List of SFX for players, gameplay and menus
        public static readonly string FIRE_DEMOLITION = "demolition_fire";
        public static readonly string FIRE_SOLDIER = "soldier_fire";
        public static readonly string FIRE_SKIRMISHER = "skirmisher_fire";        
        public static readonly string HIT = "hit";

        // List of songs
        public static readonly string BGM_MENU_MAIN = "title";
        public static readonly string BGM_MENU_SELECT_PLAYERS = "selectPlayers";
        public static readonly string BGM_MENU_LEVEL_COMPLETE = "levelComplete";
        public static readonly string BGM_GAME = "gameplay";
        

        // Data members
        protected static ISoundManager pInstance;

        protected static AudioEngine audioEngine;
        protected static WaveBank waveBank;
        protected static SoundBank soundBank;

        protected static AudioEmitter audioEmitter;
        private AudioListener listener;

        // Make a static cue such that our background music plays continuously until we tell it to stop
        protected static Cue backgroundMusic;

        
        /// <summary>
        /// Default constructor for the Sound Manager
        /// </summary>

        public SoundManager()
        {
            audioEngine = new AudioEngine("Content/Sounds/sound.xgs");
            waveBank = new WaveBank(audioEngine, "Content/Sounds/Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, "Content/Sounds/Sound Bank.xsb");

            listener = new AudioListener();
            audioEmitter = new AudioEmitter();

        }

        public void setListenerPosition(Vector3 listener)
        {
            this.listener.Position = listener;
        }

        public void updateEngine()
        {
            SoundManager.audioEngine.Update();
        }


        public void playSound(Vector3 emitterPosition, String soundName)
        {
            SoundManager.audioEmitter.Position = emitterPosition;
            Cue cue = soundBank.GetCue(soundName);
            cue.Apply3D(this.listener, SoundManager.audioEmitter);
            cue.Play();
        }

        public void playBackgroundMusic(String songName)
        {
            if( backgroundMusic != null && backgroundMusic.IsPlaying)
                backgroundMusic.Stop(AudioStopOptions.Immediate);
            backgroundMusic = soundBank.GetCue(songName);
            backgroundMusic.Play();
        }

        public void pauseBackgroundMusic()
        {
            if (backgroundMusic != null && backgroundMusic.IsPlaying)
                backgroundMusic.Pause();
        }

        public void resumeBackgroundMusic()
        {
            if (backgroundMusic != null && backgroundMusic.IsPaused)
                backgroundMusic.Resume();
        }

        /// <summary>
        /// Returns an instance of the SoundManager class, initializing it if necessary
        /// </summary>
        /// <returns>Returns an instance of the SoundManager class</returns>

        public static ISoundManager GetInstance()
        {
            if (pInstance == null)
            {
                pInstance = new SoundManager();
            }

            return pInstance;
        }

    }
}